package io.kimmking.statemachine01;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.statemachine.StateMachine;

import java.io.File;

@SpringBootApplication
public class Statemachine01Application implements ApplicationRunner {

	// https://docs.spring.io/spring-statemachine/docs/3.0.1/reference/
	// https://spring.io/projects/spring-statemachine#overview

	@Autowired
	StateMachine<TurnstileStates,TurnstileEvents> stateMachine;
	
	public static void main(String[] args) {
		SpringApplication.run(Statemachine01Application.class, args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("this is a fsm demo.");
		//System.out.println(new File("pom.xml").getAbsoluteFile().getAbsolutePath());

		stateMachine.start();
		System.out.println("current state: " + stateMachine.getState().getId());
		System.out.println("--- coin event ---");
		stateMachine.sendEvent(TurnstileEvents.COIN);
		System.out.println("current state: " + stateMachine.getState().getId());
		System.out.println("--- coin event ---");
		stateMachine.sendEvent(TurnstileEvents.COIN);
		System.out.println("current state: " + stateMachine.getState().getId());
		System.out.println("--- push event ---");
		stateMachine.sendEvent(TurnstileEvents.PUSH);
		System.out.println("current state: " + stateMachine.getState().getId());
		System.out.println("--- push event ---");
		stateMachine.sendEvent(TurnstileEvents.PUSH);
		System.out.println("current state: " + stateMachine.getState().getId());
		stateMachine.stop();
	}
}
